﻿public delegate bool GradePrint(Student s);

public class Student
{
    public string Name { get; set; }
    public int Id { get; set; }
    public Dictionary<string, double> Grades { get; set; }
    public GradePrint GP { get; set; }

    public Student(string name, int id)
    {
        Name = name;
        Id = id;
        Grades = new Dictionary<string, double>();
    }
}

public class GradeReport
{
    public static bool OrderByTerm(Student s)
    {
        if (s == null || s.Grades.Count == 0)
        {
            Console.WriteLine("无成绩数据可打印");
            return false;
        }

        Console.WriteLine($"=== {s.Name}（学号：{s.Id}）的成绩报告 ===");
        foreach (var term in s.Grades)
        {
            Console.WriteLine($"{term.Key}：{term.Value}分");
        }
        Console.WriteLine("===========================\n");
        return true;
    }

    public static bool OrderByScore(Student s)
    {
        if (s == null || s.Grades.Count == 0)
        {
            Console.WriteLine("无成绩数据可打印");
            return false;
        }

        Console.WriteLine($"=== {s.Name}（学号：{s.Id}）的成绩报告（按成绩排序） ===");
        var sortedGrades = s.Grades.OrderByDescending(g => g.Value);
        foreach (var grade in sortedGrades)
        {
            Console.WriteLine($"{grade.Key}：{grade.Value}分");
        }
        Console.WriteLine("===========================\n");
        return true;
    }
}

public class Program
{
    public static void Main()
    {
        Student student1 = new Student("张三", 1001);
        student1.Grades.Add("第一学期", 90.5);
        student1.Grades.Add("第二学期", 88.0);
        student1.Grades.Add("第三学期", 95.5);

        student1.GP = GradeReport.OrderByTerm;
        student1.GP(student1);

        student1.GP = GradeReport.OrderByScore;
        student1.GP(student1);

        Student student2 = new Student("李四", 1002);
        student2.Grades.Add("第一学期", 76.0);

        student2.GP = GradeReport.OrderByTerm;
        student2.GP(student2);
    }
}